/**
 * 类名：AdminAction
 * 创建日期： 2014-11-7 下午4:53:06
 * 
 */
package com.erms.action.admin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.JSONArray;

import com.erms.action.BaseAction;
import com.erms.bean.Courseware;
import com.erms.bean.Message;
import com.erms.bean.Teach;
import com.erms.bean.User;
import com.erms.dao.CoursewareDAO;
import com.erms.dao.MessageDAO;
import com.erms.dao.TaskDAO;
import com.erms.dao.TeachDAO;
import com.erms.dao.UserDAO;
import com.erms.domain.CoursewareService;
import com.erms.domain.MessageService;
import com.erms.util.StringUtil;

/**
 * @author hmb 备注：
 * 
 */
public class AdminAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2893263528907734739L;

	private String userKey;
	private String userJson;

	private String id;
	private String type;
	private String name;
	private String classNum;
	private String password;
	private String birthday;
	private String email;
	private String phone;
	private String degree;
	private String address;

	@Resource
	private UserDAO userDAO;

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		return SUCCESS;
	}

	public String save() {
		logger.debug(type + address);
		if ("学生".equals(type)) {
			User user = userDAO.findById(id);
			boolean legal = true;
			if (password.length() < 6 || password.length() > 20) {
				legal = false;
			}
			if ((((phone.length() == 12) || (phone.length() == 11)) && (StringUtil
					.isInteger(phone))) || phone.isEmpty()) {
			} else {
				legal = false;
			}
			if (legal) {
				user.setName(name);
				user.setClassNum(classNum);
				// user.setBirthday(birthday);
				user.setPassword(password);
				user.setEmail(email);
				user.setPhone(phone);
				user.setDormitory(address);
				userDAO.merge(user);
			}
		} else if ("教师".equals(type)) {
			User user = userDAO.findById(id);
			boolean legal = true;
			if (password.length() < 6 || password.length() > 20) {
				legal = false;
			}
			if ((((phone.length() == 12) || (phone.length() == 11)) && (StringUtil
					.isInteger(phone))) || phone.isEmpty()) {
			} else {
				legal = false;
			}
			if (legal) {
				user.setName(name);
				user.setPassword(password);
				user.setEmail(email);
				user.setPhone(phone);
				user.setOffice(address);
				userDAO.merge(user);
			}
		}
		return SUCCESS;
	}

	public String search() {
		List<User> users = new ArrayList<>();
		User tmp = userDAO.findById(userKey);
		if (tmp == null) {
		} else {
			users.add(tmp);
		}
		users.addAll(userDAO.findByName(userKey));
		List<Map<String, Object>> tempList = new ArrayList<>();
		for (User user : users) {
			Map<String, Object> map = new HashMap<>();
			map.put("id", user.getId());
			map.put("password", user.getPassword());
			map.put("name", user.getName());
			map.put("sex", user.getSex());
			map.put("birthday", user.getBirthday());
			if (user.getDepartment() == null) {
				map.put("dep", "无");
			} else {
				map.put("dep", user.getDepartment().getName());
			}
			if (user.getMajor() == null) {
				map.put("major", "无");
			} else {
				map.put("major", user.getMajor().getName());
			}
			map.put("email", user.getEmail());
			map.put("phone", user.getPhone());
			String type = user.getType();
			if (TEACHER.equals(type)) {
				if (user.getDegree() == null) {
					map.put("degree", "无");
				} else {
					map.put("degree", user.getDegree().getName());
				}
				map.put("type", "教师");
				map.put("address", user.getOffice());
			} else if (STUDENT.equals(type)) {
				map.put("address", user.getDormitory());
				map.put("type", "学生");
				map.put("classNum", user.getClassNum());
			} else if (ADMIN.equals("管理员")) {
			}
			tempList.add(map);
		}
		userJson = JSONArray.fromObject(tempList).toString();
		return SUCCESS;
	}

	public String getUserKey() {
		return userKey;
	}

	public void setUserKey(String userKey) {
		this.userKey = userKey;
	}

	public String getUserJson() {
		return userJson;
	}

	public void setUserJson(String userJson) {
		this.userJson = userJson;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getClassNum() {
		return classNum;
	}

	public void setClassNum(String classNum) {
		this.classNum = classNum;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getBirthday() {
		return birthday;
	}

	public void setBirthday(String birthday) {
		this.birthday = birthday;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getDegree() {
		return degree;
	}

	public void setDegree(String degree) {
		this.degree = degree;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

}
